package com.iwhalecloud.bss.kite.cucc.web.service.accept.core;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.cart.client.api.cart.query.IQueryKiteShoppingCartService;
import com.iwhalecloud.bss.kite.client.dto.accept.in.AcceptRequestInDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.SceneVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.scene.ICuccSceneService;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.KiteModule;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneTemplate;
import com.iwhalecloud.bss.kite.web.service.accept.service.SceneTemplateCache;
import com.ztesoft.bss.svcability.util.ListUtil;
import com.ztesoft.zsmart.core.log.ZSmartLogger;

/**
 * <Description> <br>
 * 订单录入受理入口
 *
 * @author xiao.xuzhang<br>
 * @version 1.0<br>
 * @see com.iwhalecloud.bss.kite.web.service.accept.core <br>
 * @since R9.0<br>
 */
@Service
public class LocalAcceptEngine {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private ICuccSceneService cuccSceneService;

    private static final ZSmartLogger LOGGER = ZSmartLogger.getLogger(LocalAcceptEngine.class);

    public SceneVO previewScene(String sceneInstId, String custId, String templateId, String offerId) {
        /*LoginInfo loginInfo = ContextUtil.getLoginInfo();
        Map<String, Object> extParams = loginInfo.getUserInfo().getExtParams();
        if (KiteMapUtils.isNotEmpty(extParams) && Objects.isNull(extParams.get("KITE_SCENEINST_CARTID"))) {

            Long staffId = ContextUtil.getUserId();

            // 获取购物车的分片键，调用ES进行查询，暂时先放在登录信息缓存中,方便场景加载的时候从购物车中获取数据需要通过分片键获取 ，后续改动
            IQueryKiteShoppingCartService queryKiteShoppingCartService = SpringUtil
                .getBean(IQueryKiteShoppingCartService.class);
            // 获取分片键 cartId
            Long cartId = queryKiteShoppingCartService.getShoppingCartIdByEs(staffId.toString()).getResultObject();

            if (!KiteObjectUtils.isEmpty(cartId)) {
                extParams.put("KITE_SCENEINST_CARTID", cartId.toString());
                ContextUtil.setLoginInfo(loginInfo);
            }
        }*/

        this.cuccSceneService.getCartIdFromES();

        //获取模板Id，拿到对应的处理类
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId, custId);
        SceneTemplate sceneTemplate = SceneTemplateCache.get(templateId);

        String serviceOfferId = sceneInst.getServiceOfferId();
        if ("1003".equals(serviceOfferId) || "1004".equals(serviceOfferId) || "1005".equals(serviceOfferId)) {
            // 用户停机/用户复机/用户预销户
            List<SceneModule> sceneModules = sceneTemplate.getSceneModules();
            if (!ListUtil.isEmpty(sceneModules)) {
                for (int i = 0; i < sceneModules.size(); i++) {
                    SceneModule sceneModule = sceneModules.get(i);
                    KiteModule kiteModule = sceneModule.getKiteModule();
                    if (!Objects.isNull(kiteModule)) {
                        kiteModule.setIsEdit(KeyConsts.IFTRUE_F);
                    }
                }
            }
        }

        //初始化 context
        AcceptRequestInDTO acceptRequest = new AcceptRequestInDTO();
        // 二次类业务 物联网运营新增说明信息，在预览页面需进行展示
        String showDescs = DcPublicCache.getCodea("20201216003", "showDescServiceOffer");
        List<String> showDescList = new ArrayList();
        if (KiteStringUtils.isNotEmpty(showDescs)) {
            String[] arrs = showDescs.split(SymbolConsts.COMMA);
            showDescList = Arrays.asList(arrs);
            if (showDescList.contains(serviceOfferId)) {
                acceptRequest.setSceneInstId(sceneInstId).setSceneTemplateId(templateId)
                    .setServiceOfferId(serviceOfferId).setCustId(sceneInst.getCustId())
                    .setOfferId(offerId);
            } else {
                acceptRequest.setSceneInstId(sceneInstId).setSceneTemplateId(templateId)
                    .setServiceOfferId(ServiceOfferConsts.SERVICE_OFFER_ORDER).setCustId(sceneInst.getCustId())
                    .setOfferId(offerId);
            }
        } else {
            acceptRequest.setSceneInstId(sceneInstId).setSceneTemplateId(templateId)
                .setServiceOfferId(ServiceOfferConsts.SERVICE_OFFER_ORDER).setCustId(sceneInst.getCustId())
                .setOfferId(offerId);
        }
        AcceptContext context = initContext(acceptRequest);
        context.setSceneTemplate(sceneTemplate);

        //调用模板组装
        sceneTemplate.previewNewScene(context);
        return context.getSceneVo();
    }

    private AcceptContext initContext(AcceptRequestInDTO request) {
        AcceptContext context = new AcceptContext();
        context.setAcceptRequest(request);
        return context;
    }

}
